#!/bin/bash
INSTALL_DIR=/usr/local/nges
NAGIOS_DIR=/usr/local/nagios
ES_SERVER=192.168.1.3
IDC=cp
NAGIOS_DOMAIN=ecc.rntd.cn

NG_ED=`which cfr 2>&1`
if [ $? -ne 0 ];then
        $ECHO $NG_ED
        $ECHO "Can not found command cfr in you system PATH: $PATH, pleas check it"
        exit 3
fi
ES_CMD=`which minies 2>&1`
if [ $? -ne 0 ];then
        $ECHO $ES_ED
        $ECHO "Can not found command minies in you system PATH: $PATH, pleas check it"
        exit 3
fi

JSON_CMD=`which jsonread 2>&1`
if [ $? -ne 0 ];then
        $ECHO $JSON_ED
        $ECHO "Can not found command jsonread in you system PATH: $PATH, pleas check it"
        exit 3
fi
TOOLS=${NG_ED%/*}
echo "setup nges to $INSTALL_DIR"
/usr/bin/install -c -d $INSTALL_DIR/log
/usr/bin/install -o nagios -g nagios -c -d $INSTALL_DIR/var
/usr/bin/install -o nagios -g naigos -c -d $INSTALL_DIR/var/spool
/usr/bin/install -c -d $INSTALL_DIR/var/recheck


echo "Modify Nagios to enable perfdata at $NAGIOS_DIR"
tmp=`cat $NAGIOS_DIR/etc/nagios.cfg|grep "process_performance_data=1"|wc -l `
if [ "$tmp" == "0" ]; then
echo 'process_performance_data=1' >> $NAGIOS_DIR/etc/nagios.cfg
echo ' ' >> $NAGIOS_DIR/etc/nagios.cfg
echo '# *** the template definition differs from the one in the original nagios.cfg' >> $NAGIOS_DIR/etc/nagios.cfg
echo '#' >> $NAGIOS_DIR/etc/nagios.cfg
echo "service_perfdata_file=$INSTALL_DIR/var/service-perfdata" >> $NAGIOS_DIR/etc/nagios.cfg
echo 'service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::"$SERVICEDESC$"\tSERVICEPERFDATA::"$SERVICEPERFDATA$"\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$' >> $NAGIOS_DIR/etc/nagios.cfg
echo 'service_perfdata_file_mode=a' >> $NAGIOS_DIR/etc/nagios.cfg
echo 'service_perfdata_file_processing_interval=15' >> $NAGIOS_DIR/etc/nagios.cfg
echo 'service_perfdata_file_processing_command=process-service-perfdata-file' >> $NAGIOS_DIR/etc/nagios.cfg
echo ' ' >> $NAGIOS_DIR/etc/nagios.cfg
echo '# *** the template definition differs from the one in the original nagios.cfg' >> $NAGIOS_DIR/etc/nagios.cfg
echo '#' >> $NAGIOS_DIR/etc/nagios.cfg
echo "host_perfdata_file=$INSTALL_DIR/var/host-perfdata" >> $NAGIOS_DIR/etc/nagios.cfg
echo 'host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::"$HOSTPERFDATA$"\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$' >> $NAGIOS_DIR/etc/nagios.cfg
echo 'host_perfdata_file_mode=a' >> $NAGIOS_DIR/etc/nagios.cfg
echo 'host_perfdata_file_processing_interval=15' >> $NAGIOS_DIR/etc/nagios.cfg
echo 'host_perfdata_file_processing_command=process-host-perfdata-file' >> $NAGIOS_DIR/etc/nagios.cfg
else
echo 'set service_perfdata_file in nagios.cfg'
sed -i 's/^service_perfdata_file=/\#service_perfdata_file=/g' $NAGIOS_DIR/etc/nagios.cfg
echo "service_perfdata_file=$INSTALL_DIR/var/service-perfdata" >> $NAGIOS_DIR/etc/nagios.cfg

echo 'set host_perfdata_file in nagios.cfg'
sed -i 's/^host_perfdata_file=/\#host_perfdata_file=/g' $NAGIOS_DIR/etc/nagios.cfg
echo "host_perfdata_file=$INSTALL_DIR/var/host-perfdata" >> $NAGIOS_DIR/etc/nagios.cfg
fi

echo 'Add command to support perfdata'
tmp=`$NG_ED -t -g command -n process-host-perfdata-file $NAGIOS_DIR/etc/objects/commands.cfg`
if [ "$tmp" == "0" ]; then
echo 'add  process-host-perfdata-file/process-service-perfdata-file command define'
cat << EOF >> $NAGIOS_DIR/etc/objects/commands.cfg
define command{
        command_name    process-host-perfdata-file
        command_line   /bin/mv /usr/local/nges/var/host-perfdata /usr/local/nges/var/spool/host-perfdata.$TIMET$
        }

# 'process-service-perfdata' command definition
define command{
       command_name    process-service-perfdata-file
        command_line   /bin/mv /usr/local/nges/var/service-perfdata /usr/local/nges/var/spool/service-perfdata.$TIMET$
        }
EOF
fi

$NG_ED -e -g command -n process-host-perfdata-file $NAGIOS_DIR/etc/objects/commands.cfg command_line "/bin/mv $INSTALL_DIR/var/host-perfdata $INSTALL_DIR/var/spool/host-perfdata.\$TIMET\$"
$NG_ED -e -g command -n process-service-perfdata-file $NAGIOS_DIR/etc/objects/commands.cfg command_line "/bin/mv $INSTALL_DIR/var/service-perfdata $INSTALL_DIR/var/spool/service-perfdata.\$TIMET\$"

echo 'Setup template of nagios'
tmp=`$NG_ED -t -g service -n hosts-pnp-service $NAGIOS_DIR/etc/objects/templates.cfg`
if [ "$tmp" == "0" ]; then
echo 'add hosts-pnp-service and srv-pnp-service service define'
cat  << EOF >>  $NAGIOS_DIR/etc/objects/templates.cfg
define service{
        name                            hosts-pnp-service
        use                             generic-service
        max_check_attempts              4                       ; Re-check the service up to 4 times in order to determine its final (hard) state
#        normal_check_interval           5                       ; Check the service every 5 minutes under normal conditions
        retry_check_interval            1                       ; Re-check the service every minute until a hard state can be determined
        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
        notifications_enabled   1
        normal_check_interval   3
        retry_interval  1
        process_perf_data       1
        action_url /nagios/getgraph.php?a=cp&b=$HOSTNAME$' class='tips'
        }
define service{
        name                            srv-pnp-service
        use                             generic-service
        max_check_attempts              4                       ; Re-check the service up to 4 times in order to determine its final (hard) state
        normal_check_interval           2                       ; Check the service every 5 minutes under normal conditions
        retry_check_interval            1                       ; Re-check the service every minute until a hard state can be determined
        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
        notifications_enabled   1
        normal_check_interval   2
        retry_interval  1
        process_perf_data       1
        action_url      /nagios/getgraph.php?a=cp&b=$HOSTNAME$' class='tips'
        }
EOF
fi

$NG_ED -e -g service -n hosts-pnp-service $NAGIOS_DIR/etc/objects/templates.cfg action_url "/nagios/getgraph.php?a=$IDC&b=\$HOSTNAME\$' class='tips'"
$NG_ED -e -g service -n srv-pnp-service $NAGIOS_DIR/etc/objects/templates.cfg action_url "/nagios/getgraph.php?a=$IDC&b=\$HOSTNAME\$' class='tips'"

echo 'correct same parameters....'
files='kiserver.in  ndash.in  ngpull.in  ngraph.in init.d/ngpull.in nagios/getgraph.php.in'
for i in $files; do
file=${i%.in}
echo "gen file $file from $i"
tmp=`sed "s?@nagios_dir@?$NAGIOS_DIR?g" $i`
echo "$tmp" > $file
sed -i "s?@tools@?$TOOLS?g" $file
sed -i "s?@es_server@?$ES_SERVER?g" $file
sed -i "s?@idc_name@?$IDC?g" $file
sed -i "s?@ng_dir@?$INSTALL_DIR?g" $file
sed -i "s?@nagios_domain@?$NAGIOS_DOMAIN?g" $file
done

echo 'copy files to nges and nagios'
/usr/bin/install -m 755 ngpull $INSTALL_DIR/ngpull
/usr/bin/install -m 755 ngraph $INSTALL_DIR/ngraph
/usr/bin/install -m 755 ndash  $INSTALL_DIR/ndash
/usr/bin/install -m 755 kiserver  $NAGIOS_DIR/kiserver

/usr/bin/install -m 755 init.d/ngpull /etc/init.d/ngpull

/usr/bin/install -m 644 nagios/fastphp.php  $NAGIOS_DIR/share/fastphp.php
/usr/bin/install -m 644 nagios/getgraph.php $NAGIOS_DIR/share/getgraph.php

echo 'add template to es server'
curl -XPUT $ES_SERVER:9200/_template/perfdata -d'
	{
	         "template" : "perfdata-*", 
	          "settings" : {
	              "index.refresh_interval" : "5s"
            },
	          "mappings" : {
	              "_default_" : {
	                  "_all" : {"enabled" : true, "norms" : false},
	                  "dynamic_templates" : [  {
                             "message_field" : {
                                 "path_match" : "message",
                                 "match_mapping_type" : "string",
                                 "mapping":{"type":"text", "norms" :false}
                             }
                             },
	                            {
	                                 "string_fields" : {
	                                           "match" : "*",
	                                           "match_mapping_type" : "string",
	                                           "mapping" : {
	                                                 "type" : "text", "norms" : false, 
	                                                 "fields" : {
	                                                       "keyword" : {  "type" : "keyword", "ignore_above" : 256}
	                                                 }
	                                           }
	                                 }
	                           }], 
	                  "properties" : {
	                        "@timestamp" : {"type" : "date", "include_in_all" : false}, 
	                        "idc" : { "type" : "string" ,"index":"not_analyzed"}, 
	                        "name" : { "type" : "string" ,"index":"not_analyzed"},
	                        "src_ip" : { "type" : "string" ,"index":"not_analyzed"},
	                        "severity" : {"type" : "integer"}
	                  }
	              }
	          }
	}'
